package com.yxhl.zoume.core.tripsmgmt.presenter.zoumebusticket;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.tripsmgmt.view.zoumebusticket.BusRefundTicketView;
import com.yxhl.zoume.data.http.rest.param.tripsmgnt.BusRefundParam;
import com.yxhl.zoume.data.http.rest.response.tirps.RefundTicketResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.tripsmgmt.ApplyBusRefundTicketUseCase;
import com.yxhl.zoume.domain.interactor.tripsmgmt.ConfirmBusRefundTicketUseCase;

import javax.inject.Inject;

/**
 * 项目名称：Ferrari
 * Created by menggod on 17/1/10 .
 */

public class BusTicketRefundPresenter extends BasePresenter {

    BusRefundTicketView mBusRefundTicketView;
    @Inject
    ConfirmBusRefundTicketUseCase mConfirmBusRefundTicketUseCase;
    @Inject
    ApplyBusRefundTicketUseCase mApplyBusRefundTicketUseCase;

    @Inject
    public BusTicketRefundPresenter(Context context) {
        super(context);
    }

    public void applyBusRefundTicket(String order) {
        BusRefundParam busRefundParam = new BusRefundParam();
        busRefundParam
                .setId(order)
                .setMobile(getMobile())
                .setToken(getToken());
        mApplyBusRefundTicketUseCase.execute(busRefundParam,new ZMSubscriber<RefundTicketResponse>(){
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mBusRefundTicketView.renderRefundApplyOnError(e.getMessage());
                    }

                    @Override
                    public void onNext(RefundTicketResponse refundTicketResponse) {
                        super.onNext(refundTicketResponse);

                        if (null!=refundTicketResponse){
                            if (refundTicketResponse.isSucc()) {
                                mBusRefundTicketView.renderRefundApplyOnSuccess(refundTicketResponse.getData());
                            }else{
                                mBusRefundTicketView.renderRefundApplyOnFailure(refundTicketResponse.getResultMessage());
                            }
                        }

                    }
                });

    }


    public void confirmBusRefundTicket(String order) {
        BusRefundParam busRefundParam = new BusRefundParam();
        busRefundParam
                .setId(order)
                .setMobile(getMobile())
                .setToken(getToken());
        mConfirmBusRefundTicketUseCase.execute(busRefundParam,new ZMSubscriber<RefundTicketResponse>(){
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mBusRefundTicketView.renderRefundConfirmOnError(e.getMessage());
                    }

                    @Override
                    public void onNext(RefundTicketResponse refundTicketResponse) {
                        super.onNext(refundTicketResponse);

                        if (null!=refundTicketResponse){
                            if (refundTicketResponse.isSucc()) {
                                mBusRefundTicketView.renderRefundConfirmOnSuccess("退票成功");
                            }else{
                                mBusRefundTicketView.renderRefundConfirmOnFailure(refundTicketResponse.getResultMessage());
                            }
                        }


                    }
                });
    }


    @Override
    public void attachView(BaseView view) {
        mBusRefundTicketView = (BusRefundTicketView) view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mApplyBusRefundTicketUseCase.unsubscribe();
        mApplyBusRefundTicketUseCase.unsubscribe();
    }
}
